// pages/recommendsong/recommendsong.js
import request from '../../utils/request.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    recommendList: [],
    allSongId: [],
    index: '',
    musicId: '',
    add02: '',
    ebb: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getRecommendListData();
    // 判断是否登录
    let userInFo = wx.getStorageSync('userInfo');
    if(!userInFo) {
      wx.showToast({
        title: '请登录!',
        icon: 'none',
        // 跳转至登陆界面
        success: ()=> {
          wx.reLaunch({
            url: '/pages/login/login'
          })
        }
      })
    }
  },

  async getRecommendListData() {
    let recommendListData = await request ('/recommend/songs');
    console.log(recommendListData);
    this.setData({
      recommendList: recommendListData.data.dailySongs,
    })
    // this.getRecommendListData(this.data.recommendList)
  },
  // 跳转至播放界面
  async toplay(event){
    let {song , index} = event.currentTarget.dataset;
    this.setData({
      index
    })
    // console.log(song);


    let allSongId = this.data.recommendList;
    // 路由跳转传参
    wx.navigateTo({
      url:'/pages/play/play?musicId=' + song.id,
      // 向play传递id参数
      success: (res) => {
          res.eventChannel.emit('ikunone' , {
            daa: allSongId
        })
      },
      
      // 接收play的参数
      events: {
        reverseData: (data) => {
          let { recommendList, index } = this.data;
          // 传递当前type参数(next/pre)
          console.log('reverseData', data);
          if (data.add02 == 'pre') {
            index -= 1;
            if(index <= 0){
              index = 0
            }
          } else if (data.add02 == 'next') {
            index += 1;
            if(index >= 31){
              index = 31
            }
          }
        let musicId = recommendList[index].id
          this.setData({
            data,
            musicId,
            index
          });
          let aaa = musicId
          let bbb = index
          console.log('id改变为:' + aaa)
          // 存储本地储存的切换歌曲的id
          wx.setStorageSync('key', aaa),
          wx.setStorageSync('keyIndex', bbb)
        }
      }
    })
    console.log('传输完成')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})